package top.hiai.Controller;

import java.util.Date;

import com.jfinal.plugin.activerecord.Page;

import top.hiai.config.BaseController;
import top.hiai.model.Comment;
import top.hiai.model.User;
import top.hiai.service.CommentService;
import top.hiai.util.FormatjsonUtil;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 前台用户评论控制器
*/

public class CommentController extends BaseController{
	
	CommentService commentService = new CommentService();
	/**
	 * 用户个人中心的评论列表
	 */
	public void commentListTable(){
		//获取用户的ID
		User user = getSessionAttr("LoginUserCache");
    	int uid = user.getInt("uid");
        Integer page = getParaToInt("page", 1);
        Integer limit = getParaToInt("limit", 10);
        Page<Comment> paginate = commentService.paginateGetComment(page, limit , uid);
        renderJson(FormatjsonUtil.layuiPage(paginate));
       // System.out.println(11);
	}
	/**
	 * 个人中心中的评论删除
	 */
	public void deleteComment(){
		commentService.deleteById(getParaToInt());
		redirect("/user/userCommentManage");
	}
	/**
	 * 用户给文章添加评论
	 * 1.首先判断用户是否登录了
	 * 2.登录后添加评论
	 * 返回错误信息中： 1   用户没有登录    2  评论成功
	 */
	public void commentUserArticleAdd(){
		User user = getSessionAttr("LoginUserCache");
		//Article article;
		int aid = getInt("aid");
		String content  = get("content");
		
		if(user != null){
			int uid = user.getInt("uid");
			Date comment_date =  new Date();
			getModel(Comment.class).setContent(content)
			.setComment_date(comment_date).setArticle_id(aid)
			.setUser_id(uid).save();
			renderJson("errMsg",2);
			return;
		}else{
			renderJson("errMsg",1);
			return;
		}
	}
}
